package com.tmeone.erpone.util;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tmeone.erpone.entity.SUser;
import com.tmeone.erpone.service.ISUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;

/**
 * 获取当前用户
 * 作者：wzs
 */
@Component
public class MyUser {
    @Autowired
    private ISUserService userService;

    /**
     * 获取当前用户对象
     * 作者：wzs
     * @return 当前用户
     */
    public SUser getUser() {
        UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        QueryWrapper<SUser> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("user_name",userDetails.getUsername());
        return userService.getOne(queryWrapper1);
    }
}
